package com.heartware.sh.blackhole.ui.activity;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.heartware.sh.blackhole.R;
import com.heartware.sh.blackhole.ui.activity.base.BaseActivity;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;

/**
 * Created by Administrator on 2018/4/2 0002.
 */

public class TenActivity extends BaseActivity implements View.OnClickListener{

    private static final String TAG = TenActivity.class.getSimpleName();

    private Button btn_create;
    private Button btn_from;
    private Button btn_just;

    private Button btn_map;
    private Button btn_flatMap;

    private Button btn_zip;
    private Button btn_zipWith;

    private Button btn_retry;
    private Button btn_retryWhen;

    private Button btn_filter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_ten;
    }

    @Override
    public void initView() {
        super.initView();

        /**
         *
         * create       创建
         * from         创建
         * just         创建
         * map          转换
         * flatMap      转换
         * zip          合并
         * zipWith      合并
         * retry        重试
         * retryWhen    重试
         * filter       过滤
         * timer        定时
         *
         */

        btn_create = (Button) findViewById(R.id.btn_create);
        btn_create.setOnClickListener(this);

        btn_from = (Button) findViewById(R.id.btn_from);
        btn_from.setOnClickListener(this);

        btn_just = (Button) findViewById(R.id.btn_just);
        btn_just.setOnClickListener(this);

        btn_map = (Button) findViewById(R.id.btn_map);
        btn_map.setOnClickListener(this);

        btn_flatMap = (Button) findViewById(R.id.btn_flatMap);
        btn_flatMap.setOnClickListener(this);

        btn_zip = (Button) findViewById(R.id.btn_zip);
        btn_zip.setOnClickListener(this);

        btn_zipWith = (Button) findViewById(R.id.btn_zipWith);
        btn_zipWith.setOnClickListener(this);

        btn_retry = (Button) findViewById(R.id.btn_retry);
        btn_retry.setOnClickListener(this);

        btn_retryWhen = (Button) findViewById(R.id.btn_retryWhen);
        btn_retryWhen.setOnClickListener(this);

        btn_filter = (Button) findViewById(R.id.btn_filter);
        btn_filter.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.btn_create:
                create();
                break;
            case R.id.btn_from:
                from();
                break;
            case R.id.btn_just:
                just();
                break;
            case R.id.btn_map:
                map();
                break;
            case R.id.btn_flatMap:
                flatMap();
                break;
            case R.id.btn_zip:
                zip();
                break;
            case R.id.btn_zipWith:
                zipWith();
                break;
            case R.id.btn_retry:
                retry();
                break;
            case R.id.btn_retryWhen:
                retryWhen();
                break;
            case R.id.btn_filter:
                filter();
                break;
            default:
                break;
        }

    }

    private void filter() {
        Observable.just(1,2,3,4,5)
                .filter(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer integer) {
                        return integer%2 == 1;
                    }
                }).subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call===filter==="+integer);
                    }
                });

        Observable.just(1,2,3,4,5)
                .map(new Func1<Integer, Bitmap>() {
                    @Override
                    public Bitmap call(Integer integer) {

                        Bitmap bitmap = null;

                        try {
                            bitmap = Glide.with(TenActivity.this).load(""+1).asBitmap().into(500,500).get();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        }

                        return bitmap;

                    }
                });
    }

    private void retryWhen() {

        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {

                try {

                    throw new Exception("总是出错！");

                }catch (Exception e){

                    subscriber.onError(e);

                }

            }
        }).retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
            @Override
            public Observable<?> call(Observable<? extends Throwable> observable) {

                return observable.zipWith(Observable.range(1, 3), new Func2<Throwable, Integer, Integer>() {
                    @Override
                    public Integer call(Throwable throwable, Integer integer) {
                        return integer;
                    }
                }).flatMap(new Func1<Integer, Observable<?>>() {
                    @Override
                    public Observable<?> call(Integer integer) {
                        Log.i(TAG, "间隔=" + integer + "=秒");
                        return Observable.timer(integer, TimeUnit.SECONDS);
                    }
                });
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {
                Log.i(TAG, "onCompleted()");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG,"onError()==="+e.getMessage());
            }

            @Override
            public void onNext(String s) {
                Log.i(TAG, "onNext()"+s);
            }
        });

    }

    private void retry() {

        Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {

                try {

                    for (int i = 0; i < 5; i++) {

                        if (i == 3) {
                            throw new Exception("出错啦！");
                        }

                        subscriber.onNext(i);

                    }
                    subscriber.onCompleted();
                }catch (Exception e){
                    subscriber.onError(e);
                }

            }
        }).retry(2).subscribe(new Observer<Integer>() {
            @Override
            public void onCompleted() {
                Log.i(TAG, "onCompleted()");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError()==="+e.getMessage());
            }

            @Override
            public void onNext(Integer integer) {
                Log.i(TAG, "onNext()==="+integer);
            }
        });

    }

    private void zipWith() {

        Observable<Integer> observable = Observable.from(new Integer[]{1,2,3,4,5});
        Observable<String> observable1 = Observable.just("one", "two", "three", "four", "five");

        observable.zipWith(observable1, new Func2<Integer, String, String>() {
            @Override
            public String call(Integer integer, String s) {
                return "zipWith=="+integer+"==="+s;
            }
        }).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.i(TAG, "call==="+s);
            }
        });

    }

    private void zip() {

        Observable<Integer> observable = Observable.from(new Integer[]{1,2,3,4,5});
        Observable<String> observable1 = Observable.just("one", "two", "three", "four", "five");

        Observable.zip(observable, observable1, new Func2<Integer, String, String>() {
            @Override
            public String call(Integer integer, String s) {
                return "zip=="+integer+"==="+s;
            }
        }).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.i(TAG, "call==="+s);
            }
        });

    }

    private void flatMap() {

        Observable.just(1,2,3,4,5)
                .flatMap(new Func1<Integer, Observable<Weather>>() {
                    @Override
                    public Observable<Weather> call(Integer integer) {

                        Weather weather = new Weather();
                        weather.cityId = integer;
                        weather.weather = integer%2 == 0?"晴天":"阴天";

                        return Observable.just(weather);
                    }
                }).subscribe(new Action1<Weather>() {
                    @Override
                    public void call(Weather weather) {
                        Log.i(TAG, "call===flatMap==="+weather.toString());
                    }
                });


    }

    private void map() {

        Observable.just(1,2,3,4,5)
                .map(new Func1<Integer, Weather>() {
                    @Override
                    public Weather call(Integer integer) {

                        Weather weather = new Weather();
                        weather.cityId = integer;
                        weather.weather = integer%2 == 0?"晴天":"阴天";

                        return weather;
                    }
                }).subscribe(new Action1<Weather>() {
                    @Override
                    public void call(Weather weather) {
                        Log.i(TAG, "call===map==="+weather.toString());
                    }
                });

    }

    class Weather{
        public int cityId;
        public String weather;

        @Override
        public String toString() {
            return "{"+cityId+"==="+weather+"}";
        }
    }

    private void just() {

        Observable.just(1,2,3,4,5)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call===just==="+integer);
                    }
                });

    }

    private void from() {

        Integer[] ints = {1,2,3,4,5};
        Observable<Integer> observable = Observable.from(ints);

        observable.subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.i(TAG, "call===from==="+integer);
            }
        });

    }

    private void create() {

        Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("create==="+1);
                subscriber.onNext("create==="+2);
                subscriber.onNext("create==="+3);
                subscriber.onNext("create==="+4);
                subscriber.onNext("create==="+5);
                subscriber.onCompleted();
            }
        });


        Subscriber<String> subscriber = new Subscriber<String>() {
            @Override
            public void onCompleted() {
                Log.i(TAG, "onCompleted()");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError()");
            }

            @Override
            public void onNext(String s) {
                Log.i(TAG, "onNext()==="+s);
            }
        };

        observable.subscribe(subscriber);

    }
}
